# -*- coding:utf8 -*-

# __author__ = "zhangying"
# __date__ = "2019/2/22 9:39"
# 操作 config中userconfig.yaml 文件中的端口号信息
#根据设备有多少个，启动线程appium服务

import yaml


class WriteUserCommand:
    def read_data(self):
        """
       加载yaml数据
       :return:
       """
        with open("../config/userconfig.yaml",encoding='utf-8') as fr:
            data = yaml.load(fr)
        return data

    def get_value(self,key,port):
        """
        获取value
        :return:
        """
        data = self.read_data()
        value = data[key][port]
        return value

    def write_data(self,i,device,bp,port):
        '''
        写入数据，调用join_data将拼接好的数据写入
        :param i:
        :param device:
        :param bp:
        :param port:
        :return:
        '''
        data = self.join_data(i,device,bp,port)
        with open("../config/userconfig.yaml",'a',encoding='utf-8') as fr:
            yaml.dump(data,fr)

    def join_data(self,i,device,bp,port):
        """
        拼接数据user_info_0: {bp: '4900', deviceName: '127.0.0.1:21503', port: '4700'}
        :param i:
        :param device:
        :param bp:
        :param port:
        :return:
        """
        data = {
            "user_info_"+str(i):{
                "deviceName":device,
                "bp":bp,
                "port":port
            }
        }
        return data

    def clear_data(self):
        """
        清空数据
        :return:
        """
        with open("../config/userconfig.yaml",'w',encoding='utf-8') as fr:
            fr.truncate()
        fr.close()

    def get_file_lines(self):
        '''
        获取yaml文件中有几个设备信息
        :return:
        '''
        data = self.read_data()
        return len(data)



if __name__ == '__main__':
    write_file = WriteUserCommand()
    print(write_file.get_value('user_info_0','bp'))




